package com.systop.tea.config;

import com.systop.tea.interceptor.LoginInterceptor;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.io.File;

@Configuration
public class MvcConfig implements WebMvcConfigurer {

    // 静态资源放行
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/page/**").addResourceLocations("classpath:/page/");
        registry.addResourceHandler("/plugins/**").addResourceLocations("classpath:/plugins/");
        registry.addResourceHandler("/css/**").addResourceLocations("classpath:/css/");
        registry.addResourceHandler("/js/**").addResourceLocations("classpath:/js/");
        registry.addResourceHandler("/video/**").addResourceLocations("classpath:/video/");
        registry.addResourceHandler("/images/**").addResourceLocations("classpath:/images/");

        //获取jar包所在目录
        // ApplicationHome h = new ApplicationHome(getClass());
        // File jarF = h.getSource();
        // //在jar包所在目录下生成一个upload文件夹用来存储上传的图片
        // String dirPath = jarF.getParentFile().toString()+"/images/";
        // System.out.println("jar包所在目录下--->" + dirPath);
        //
        // registry.addResourceHandler("/images/**").addResourceLocations("file:" + dirPath);
    }

    // 拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        LoginInterceptor loginInterceptor = new LoginInterceptor();
        registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/css/**")
                .excludePathPatterns("/images/**")
                .excludePathPatterns("/video/**")
                .excludePathPatterns("/js/**")
                .excludePathPatterns("/plugins/**")
                .excludePathPatterns("/page/login.html")
                .excludePathPatterns("/admin/login")
                .excludePathPatterns("/admin/logout")
                .excludePathPatterns("/banner/query")
                .excludePathPatterns("/category/query")
                .excludePathPatterns("/product/**")
                .excludePathPatterns("/cart")
                .excludePathPatterns("/cart/**")
                .excludePathPatterns("/order")
                .excludePathPatterns("/order/**")
                .excludePathPatterns("/article")
                .excludePathPatterns("/article/**")
                .excludePathPatterns("/user")
                .excludePathPatterns("/user/**")
                .excludePathPatterns("/address")
                .excludePathPatterns("/address/**");
    }

}
